package edu.uwm.universitydb.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import edu.uwm.universitydb.swing.PerformanceTableModel;


public class PerformanceMaint  extends JPanel {

	public PerformanceMaint(PerformanceTableModel tableModel) {
		_tableModel = tableModel;
		this.setLayout(new BorderLayout());
		
		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(new BorderLayout());
		titlePanel.setBackground(Color.WHITE);
		titlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, 16, 0, Color.WHITE));
		JLabel title = new JLabel("Performance Testing");
		title.setFont(new Font(title.getFont().getName(), title.getFont().getStyle(), 18));
		titlePanel.add(title);

		this.add(titlePanel, BorderLayout.NORTH);
		this.add(this.createAppView(), BorderLayout.CENTER);
		
	}
	
	private JPanel createAppView() {
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		//Create run button panel
		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		
		_runButton = new JButton(PerformanceMaint.RUNBUTTON);
		_runButton.setIcon(new ImageIcon("res/run.png"));
		_runButton.setName(PerformanceMaint.RUNBUTTON);
		
		btnPanel.setBackground(Color.WHITE);
		btnPanel.add(_runButton);
		panel.add(btnPanel);

		//Create Table
		_datatable = new JTable(_tableModel);
		_datatable.setAlignmentX(Component.CENTER_ALIGNMENT);
		_datatable.getColumnModel().getColumn(0).setMaxWidth(500);
		_datatable.getColumnModel().getColumn(0).setMinWidth(500);
		_datatable.getColumnModel().getColumn(1).setMaxWidth(150);
		_datatable.getColumnModel().getColumn(2).setMaxWidth(150);
		_datatable.getTableHeader().setReorderingAllowed(false);

		JScrollPane tblPane = new JScrollPane(_datatable);
		tblPane.getViewport().setBackground(Color.WHITE);
		panel.add(tblPane);
		
		panel.add(Box.createRigidArea(new Dimension(0,16)));
		return panel;
	}
	
	public JButton getRunButton() {
		return _runButton;
	}
	
	public JTable getDataTable() {
		return _datatable;
	}
	
	public void refreshTable() {
	    _datatable.revalidate();
	    _datatable.repaint();		
	}
	
	private JButton _runButton;
	private JTable _datatable;
	
	private PerformanceTableModel _tableModel;

	public static final String RUNBUTTON = "Run";

	private static final long serialVersionUID = 1935956530047802788L;

}
